package cn.otrue.patienthealthmanager.view;

import java.util.Calendar;
import java.util.Date;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager.LayoutParams;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import cn.otrue.patienthealthmanager.R;
import cn.otrue.patienthealthmanager.utils.CommonUtil;
import cn.otrue.patienthealthmanager.utils.DateFormatUtils;
import cn.otrue.patienthealthmanager.view.CalendarView.OnItemClickListener2;
import cn.otrue.patienthealthmanager.view.CalendarView.OnMonthChangeListener;

/**
 * 订制化的PopWindow，用于弹出三种选择器（城市、日期、年龄还有性别……）
 * 
 * @author wujiankun
 * 
 */
public class MyPopupWindow extends PopupWindow {
	private Context mContext;
	/** 省市区的统合描述(或者是选择好的出生日期、年龄) */
	public String desc;

	private Date currnetDate;

	private String[] args;

	public void setAdressArrs(String[] adressArrs) {
		this.args = adressArrs;
	}

	private int textSize;
	private View mPopView;

	private GetPickResultListener getPickResultListener;

	public MyPopupWindow(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public MyPopupWindow(Activity context,
			final GetPickResultListener getPickResultListener,
			final int layoutId, Date currnetDate, String[] adressArrs) {

		super(context);
		this.args = adressArrs;
		this.currnetDate = currnetDate;
		this.getPickResultListener = getPickResultListener;
		mContext = context;
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mPopView = inflater.inflate(layoutId, null);

		textSize = CommonUtil.dip2px(mContext, 20);

		initWindowStyleByVersion(mPopView);
		// 设置SelectPicPopupWindow的View
		this.setContentView(mPopView);
		// 设置SelectPicPopupWindow弹出窗体的宽
		this.setWidth(LayoutParams.MATCH_PARENT);
		// 设置SelectPicPopupWindow弹出窗体的高
		this.setHeight(LayoutParams.WRAP_CONTENT);
		// 设置SelectPicPopupWindow弹出窗体可点击
		this.setFocusable(false);
		// 设置SelectPicPopupWindow弹出窗体动画效果
		this.setAnimationStyle(R.style.AnimBottom);
		this.setOutsideTouchable(true);

		if (layoutId == R.layout.item_pick_date_layout) {
			initPickDateWindow(mPopView);
		}

	}

	/**
	 * 根据版本不同，设置不同的弹框样式
	 * 
	 * @param versionId
	 * @param mPopView2
	 * @param tv_catt_cancle
	 * @param tv_catt_ok
	 */
	private void initWindowStyleByVersion(View mPopView2) {
		// 窗口标题容器
		RelativeLayout rl_catt_popTitle = (RelativeLayout) mPopView2
				.findViewById(R.id.rl_catt_popTitle);
		// 窗口标题
		TextView tv_catt_windowTitle = (TextView) mPopView2
				.findViewById(R.id.tv_catt_windowTitle);

		// int dip2px = dip2px(mContext, 18);
		rl_catt_popTitle.setBackgroundColor(mContext.getResources().getColor(
				R.color.window_bg_children));
		tv_catt_windowTitle.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
		// tv_catt_cancle.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);

	}

	/**
	 * 初始化并准备日历选择控件
	 * 
	 * @param
	 */
	public void initPickDateWindow(View mPopView2) {

		final TextView tv_catt_windowTitle = (TextView) mPopView2
				.findViewById(R.id.tv_catt_windowTitle);
		desc = DateFormatUtils.format(currnetDate, "yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		cal.setTime(currnetDate);
		int month = cal.get(Calendar.MONTH) + 1;
		tv_catt_windowTitle.setText(cal.get(Calendar.YEAR) + "年" + month + "月");
		// 获取日历控件对象

		final CalendarView calendar2 = (CalendarView) mPopView2
				.findViewById(R.id.calendar);

		calendar2.setExtraData(currnetDate);
		mPopView2.findViewById(R.id.iv_catt_nextMonth).setOnClickListener(
				new OnClickListener() {
					@Override
					public void onClick(View v) {// 下一月
						String[] lya = calendar2.clickRightMonth().split("-");
						tv_catt_windowTitle
								.setText(lya[0] + "年" + lya[1] + "月");
						desc = lya[0] + "年" + lya[1] + "月";
					}
				});
		mPopView2.findViewById(R.id.iv_catt_lastMonth).setOnClickListener(
				new OnClickListener() {
					@Override
					public void onClick(View v) {// 上一月
						String[] ya = calendar2.clickLeftMonth().split("-");
						tv_catt_windowTitle.setText(ya[0] + "年" + ya[1] + "月");
						desc = ya[0] + "年" + ya[1] + "月";
					}
				});

		calendar2.setOnItemClickListener(new OnItemClickListener2() {
			@Override
			public void OnItemClick(Date date) {
				desc = DateFormatUtils.format(date, "yyyy-MM-dd");
				currnetDate = date;
				Calendar cal = Calendar.getInstance();
				cal.setTime(currnetDate);
				int month = cal.get(Calendar.MONTH) + 1;
				tv_catt_windowTitle.setText(cal.get(Calendar.YEAR) + "年"
						+ month + "月");
				getPickResultListener.onGetResult(new Object[] { desc, date },
						2);
				dismiss();
			}
		});
		calendar2.setMonthChangeListener(new OnMonthChangeListener() {
			@Override
			public void onMonthChange(String[] ym) {
				tv_catt_windowTitle.setText(ym[0] + "年" + ym[1] + "月");
				Log.e("Popup", "setMonthChangeListener");
			}
		});

	}

}
